package com.microservice.educenter.controller;


import com.microservice.commonutils.JwtUtils;
import com.microservice.commonutils.R;
import com.microservice.educenter.entity.UcenterMember;
import com.microservice.educenter.entity.vo.RegisterVo;
import com.microservice.educenter.entity.vo.UserInfoVo;
import com.microservice.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author leesin
 * @since 2022-05-12
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    @GetMapping("getALL")
    public R getAll(){
        List<UcenterMember  > ucenterMemberList=memberService.list(null);
        return R.ok().data("list",ucenterMemberList);
    }

    //登陆
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member){

        String token =memberService.login(member);
        return R.ok().data("token",token);
    }
    //注册
    @PostMapping("register")
    public  R registerUser (@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();

    }
    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

    @GetMapping("getUserInfoById/{id}")
    public UserInfoVo getUserInfoById(@PathVariable(value = "id") String id){
        UcenterMember byId = memberService.getById(id);
        UserInfoVo userInfoVo=new UserInfoVo() ;

//        userInfoVo.setNickname(byId.getNickname());
//        userInfoVo.setAvatar(byId.getAvatar());
//        userInfoVo.setAge(byId.getAge());
//        userInfoVo.setSex(byId.getSex());

        BeanUtils.copyProperties(byId,userInfoVo);

        return userInfoVo;
    }

}

